package com.jly.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jly.Util.Constants;
import com.jly.Util.ResponseData;
import com.jly.Util.util;
import com.jly.service.RedisService;
import com.jly.service.RedisService4;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping(value = "/push",method = RequestMethod.POST)
public class PushController {

    private Logger logger = LoggerFactory.getLogger(PushController.class);

    @Autowired
    private RedisService redisService;

    @Autowired
    private RedisService4 redisService4;

    /**
     * 推送验证码
     * @return
     */
    @RequestMapping("sendAuthCode")
    public Object sendAuthCode(String openid){
        ResponseData result = new ResponseData();
        try {
            String code = util.generateRandomNumber(6)+"";
            String access_token = util.getToken(redisService);
            String[] arr = {"name", "code"};
            String[] arr2 = {"【经理云】", code};
            JSONObject json = new JSONObject();
            json.put("access_token", access_token);
            json.put("openid", openid);
            json.put("TemplateId", Constants.AuthCodeId);
            JSONObject jsons = util.pushMessage(json,arr,arr2,redisService4);
            result = JSON.toJavaObject(jsons, ResponseData.class);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Constants.Fail_400);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    /**
     * 推送单据审核
     * @return
     */
    @RequestMapping("sendAuthTicket")
    public Object sendAuthTicket(String id){
        ResponseData result = new ResponseData();
        try {
            JSONObject json = JSON.parseObject(id);
            String ity_Number = json.getString("ity_Number");
            Integer ity_type = json.getInteger("ity_type");
            Integer store_id = json.getInteger("store_id");
            JSONObject jsons = new JSONObject();
            jsons.put("accesstoken", "select user_wechat,user_realname,user_remarks from view_us_u_level where store_id=" + store_id+" and level in (201,202,203)");
            jsons.put("code","A11");
            json = util.sendPost(Constants.URL + "/common/api.do", jsons);
            result.setCode(Constants.SUCCESS_200);
            if(400==json.getInteger("code")){
                result.setCode(Constants.Fail_400);
                result.setMsg(json.getString("msg"));
            }else if(200==json.getInteger("code")){
                JSONArray array = json.getJSONArray("data");
                for (int i = 0; i < array.size(); i++) {
                    String openid = array.getJSONObject(i).getString("user_wechat");
                    String user_realname = array.getJSONObject(i).getString("user_realname");
                    String push_status = array.getJSONObject(i).getString("user_remarks");
                    if("-1".equals(push_status)){//推送关闭则不推送
                        continue;
                    }
                    if(util.isEmpty(openid)){
                        continue;
                    }
                    JSONObject jsonobj = new JSONObject();
                    jsonobj.put("access_token", util.getToken(redisService));
                    jsonobj.put("openid", openid);
                    jsonobj.put("TemplateId", Constants.TicketTemplateId);
                    jsonobj.put("url",redisService.getStringValue("redirect_url2")+"?ity_Number="+ity_Number+"&ity_type="+ity_type);
                    String[] arr = {"title", "number", "time", "remark"};
                    String[] arr2 = {user_realname + ",您好!您正在进行" + Constants.TicketObj[ity_type] + "审核操作"
                                    ,ity_Number,util.getTimeString(new Date(),"yyyy-mm-dd HH:mm:ss"),
                            "请及时核对----【回复QXTS取消订单推送】"};
                    json = util.pushMessage(jsonobj, arr, arr2, redisService4);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Constants.Fail_400);
            result.setMsg(e.getMessage());
        }
        return result;
    }

}
